/*
 * Copyright 2008 Slava Chernyak
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License. 
 */
package com.chernyak.DataAdapter.types;

public class StringDecl extends TypeDecl {

	public StringDecl(String name, boolean compare, int ln) {
		super(name, compare, ln);
	}

	@Override
	public void lowerDeclarations(StringBuilder sink) {
		sink.append("public String " + name + " = null;");
	}

	@Override
	public void lowerEquals(StringBuilder sink) {
		sink.append("__da_test &= " + this.name + ".equals(__da_o);");
	}

	public void lowerComparisonWCG(StringBuilder sink) {
		if (compare) {
			sink.append("__da_c_result = (__da_c_result == 0) ? " + name + ".compareTo(__da_o." + 
			    name + ") : __da_c_result;");
		}
	}
	
	public void lowerHashCode(StringBuilder sink) {
		if (compare) {
			sink.append("__da_hashcode += (null != " + name + ") ? " + name + ".hashCode() : -1;");
		}
	}
	
	public void lowerToString(StringBuilder sink) {
		lowerToString(sink, 0);
	}
	
	protected void lowerToString(StringBuilder sink, int arrayDim) {
		sink.append("__da_str += ((" + name + " != null) ? " + name +
				genArrayDeref(arrayDim) + " : \"null\" ) + __DA_STRING_DELIM;");
	}
	
	public void lowerReadWCG(StringBuilder sink) {
		lowerRead(sink, 0);
	}

	public void lowerWriteWCG(StringBuilder sink) {
		lowerWrite(sink, 0);
	}

	protected void lowerRead(StringBuilder sink, int arrayDim) {
		sink.append("if (d.readBoolean() == false) {");
		sink.append(	name + genArrayDeref(arrayDim) + " = null;");
		sink.append("} else {");
		sink.append(	"StringBuilder __da_sb = new StringBuilder();");
		sink.append(	"char __da_c = d.readChar();");
		sink.append(	"while(__da_c != 0) {");
		sink.append(		"__da_sb.append(__da_c);");
		sink.append(		"__da_c = d.readChar();");
		sink.append(	"}");
		sink.append(	name + genArrayDeref(arrayDim) + " = __da_sb.toString();");
		sink.append("}");
	}

	protected void lowerWrite(StringBuilder sink, int arrayDim) {
		sink.append("if (" + name + genArrayDeref(arrayDim) + " == null) {");
		sink.append(	"d.writeBoolean(false);");
		sink.append("} else {");
		sink.append(	"d.writeBoolean(true);");
		sink.append(	"char[] __da_ch = " + name + genArrayDeref(arrayDim) + ".toCharArray();");
		sink.append(	"for(int j = 0; j < __da_ch.length; j++) {");
		sink.append(		"d.writeChar(__da_ch[j]);");
		sink.append(	"}");
		sink.append(	"d.writeChar((char)0);");
		sink.append("}\n");
	}
	

	public void lowerParse(StringBuilder sink) {
		lowerParse(sink, 0);
	}
	
	protected void lowerParse(StringBuilder sink, int arrayDim) {
		if (arrayDim != 1) { 
			sink.append("__da_o." + name + genArrayDeref(arrayDim) + " = ");
		}
		sink.append("__da_tokens.nextToken();");
	}
	
	protected String getTypeName() {
		return "String";
	}
	
	protected String getBoxedTypeName() {
		return "String";
	}
}
